package handler

import (
	"bytes"
	"encoding/binary"
	"fmt"
	"github.com/antchfx/htmlquery"
	"github.com/gocolly/colly"
	"log"
	"sbd_nydjz/models"
	"sbd_nydjz/service"
	"sbd_nydjz/utils"
	"strings"
)

type GasterHandler struct {
	GasterSrv service.GasterService
	CfInfoSrv service.CfInfoService
}

func (handler GasterHandler) InsterGasterHandler(id string) {
	isGaster := &models.Djzinfo{}
	isCfInfo := &models.Cfinfo{}
	// fmt.Println(id)
	url := fmt.Sprintf("https://www.icama.cn/BasicdataSystem/pesticideRegistration/viewpd.do?id=%s", id)
	// fmt.Println(url)
	// isGaster := &models.Djzinfo{}
	c := colly.NewCollector(colly.UserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"))
	c.OnRequest(func(r *colly.Request) {
		r.Headers.Set("Cookie", "JSESSIONID=D4F6BAB7AE0FC35310CF83EB3ECDF014; pageSize=30; pageNo=1")
		// r.Headers.Set("Content-Type", " application/x-www-form-urlencoded")

	})
	c.OnResponse(func(r *colly.Response) {
		doc, err := htmlquery.Parse(strings.NewReader(string(r.Body)))
		if err != nil {
			log.Fatal(err)
		}
		nodes := htmlquery.Find(doc, `/html/body/div[1]/table[1]/tbody/tr`)
		// html/body/div[1]
		for key, node := range nodes {
			// fmt.Println(key)
			if key == 1 {
				// str := htmlquery.OutputHTML(node, false)
				// 登记证号
				Djzh := htmlquery.FindOne(node, "./td[2]")
				isGaster.Djzh = utils.ReplaceStr(htmlquery.InnerText(Djzh))
				isCfInfo.Djzh = utils.ReplaceStr(htmlquery.InnerText(Djzh))
				// 首次批准日期：
				Starttime := htmlquery.FindOne(node, "./td[4]")
				isGaster.Starttime = utils.ReplaceStr(htmlquery.InnerText(Starttime))
				// 有效期至：
				Endtime := htmlquery.FindOne(node, "./td[6]")
				isGaster.Endtime = utils.ReplaceStr(htmlquery.InnerText(Endtime))

				// fmt.Println(isGaster.Starttime)
			}
			if key == 2 {
				// 农药名称：
				Nymc := htmlquery.FindOne(node, "./td[2]")
				isGaster.Nymc = utils.ReplaceStr(htmlquery.InnerText(Nymc))
				// 毒性：
				Dx := htmlquery.FindOne(node, "./td[4]")
				isGaster.Dx = utils.ReplaceStr(htmlquery.InnerText(Dx))
				// 剂型：
				Jx := htmlquery.FindOne(node, "./td[6]")
				isGaster.Jx = utils.ReplaceStr(htmlquery.InnerText(Jx))

			}
			if key == 4 {
				// 农药类别：
				Nylb := htmlquery.FindOne(node, "./td[2]")
				isGaster.Nylb = utils.ReplaceStr(htmlquery.InnerText(Nylb))
			}
			if key == 5 {
				// 备注：
				Comment := htmlquery.FindOne(node, "./td[2]")
				isGaster.Comment = utils.ReplaceStr(htmlquery.InnerText(Comment))

			}

			// code = append(code, htmlquery.InnerText(a))
			// viewpd = append(viewpd, str[:len(str)-2][9:])
		}
		isGaster.ID = utils.SnowId()

		cfnode := htmlquery.Find(doc, `/html/body/div[1]/table[3]/tbody/tr`)
		// fmt.Println(htmlquery.OutputHTML(cfnode[0], false))
		for key, node := range cfnode {
			// fmt.Println(key)
			if key < 2 {
				continue
			}
			// 农药名称：
			Yxcf := htmlquery.FindOne(node, "./td[1]")
			isCfInfo.Yxcf = utils.ReplaceStr(htmlquery.InnerText(Yxcf))
			// fmt.Println(isCfInfo.Yxcf)
			// 毒性：
			Yfm := htmlquery.FindOne(node, "./td[2]")
			isCfInfo.Yfm = utils.ReplaceStr(htmlquery.InnerText(Yfm))
			// 剂型：
			Yxcfhl := htmlquery.FindOne(node, "./td[3]")
			isCfInfo.Yxcfhl = utils.ReplaceStr(htmlquery.InnerText(Yxcfhl))
			isCfInfo.ID = utils.SnowId()
			handler.CfInfoSrv.InsterCfInfoSrv(*isCfInfo)
			fmt.Println(isCfInfo)
		}

		handler.GasterSrv.InsterGasterHandler(*isGaster)
	})
	c.Visit(url)
}

func CfInfoByte(isCfinfo models.Cfinfo) []byte {
	buf := &bytes.Buffer{}
	err := binary.Write(buf, binary.LittleEndian, isCfinfo)
	if err != nil {
		panic(err)
	}
	return buf.Bytes()
}

//
// func getGaster(id string) (isGaster models.Djzinfo) {
// 	url := fmt.Sprintf("https://www.icama.cn/BasicdataSystem/pesticideRegistration/viewpd.do?id=%s", id)
// 	// fmt.Println(url)
// 	// isGaster := &models.Djzinfo{}
// 	c := colly.NewCollector(colly.UserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"))
// 	c.OnRequest(func(r *colly.Request) {
// 		r.Headers.Set("Cookie", "JSESSIONID=D4F6BAB7AE0FC35310CF83EB3ECDF014; pageSize=30; pageNo=1")
// 		// r.Headers.Set("Content-Type", " application/x-www-form-urlencoded")
//
// 	})
// 	c.OnResponse(func(r *colly.Response) {
// 		doc, err := htmlquery.Parse(strings.NewReader(string(r.Body)))
// 		if err != nil {
// 			log.Fatal(err)
// 		}
// 		nodes := htmlquery.Find(doc, `/html/body/div[1]/table[1]/tbody/tr`)
// 		// html/body/div[1]
// 		for key, node := range nodes {
// 			// fmt.Println(key)
// 			if key == 1 {
// 				str := htmlquery.OutputHTML(node, false)
// 				fmt.Println(str)
// 			}
//
// 			// code = append(code, htmlquery.InnerText(a))
// 			// viewpd = append(viewpd, str[:len(str)-2][9:])
// 		}
// 	})
// 	c.Visit(url)
// }
